/**
 * Create by richliu1023
 * @date 2016-06-07
 * @email richliu1023@gmail.com
 * @github https://github.com/RichLiu1023
 * @description a simple explanation
 */
class TopNoticeUI extends rm.EventDispatcher {

	public static COMPLETE:number = 0;

	private _isShow:boolean = false;
	private _speed:number = 0.08;
	private _textField:egret.TextField;

	public constructor() {
		super();
	}

	public execute( txt:string, start:number, end:number, parent:egret.DisplayObjectContainer ):void {
		if ( !parent )return egret.log( 'error: TopNoticeUI Parent Is Null!' );
		if ( this._isShow )return egret.log( 'error: TopNoticeUI Showed!' );
		var textField = this.getTextField( txt );
		parent.addChild( textField );
		this.startAction( textField, start, end );
	}

	private getTextField( txt ):egret.TextField {
		if ( !this._textField ) {
			this._textField = new egret.TextField();
			this._textField.y = 50;
		}
		this._textField.textFlow = rm.htmlParser.parser( txt );
		return this._textField;
	}

	private startAction( textField:egret.TextField, start:number, end:number ):void {
		this._isShow = true;
		Tween.removeTweens( textField );
		textField.x = start;
		end = end - textField.textWidth;
		var distance = Math.abs( Math.abs( start ) - Math.abs( end ) );
		var time = distance / this._speed;
		Tween.get( textField ).to( { x: end }, time ).call( this.complete, this, [ textField ] );
	}

	private complete( textField:egret.TextField ):void {
		this._isShow = false;
		textField.parent && textField.parent.removeChild( textField );
		textField.textFlow = null;
		this.dispatchEvent( TopNoticeUI.COMPLETE );
	}

	get isShow():boolean {
		return this._isShow;
	}
}